
(function (w, doc) {
  function _addEvent(target, type, handel) {
    if (w.addEventListener) {
      target.addEventListener(type, e => {
        if (typeof handel === 'function' && handel() === false) {
          e.cancelBubble = true
        }
      }, false)
    } else {
      target.attachEvent('on' + type, e => {
        if (typeof handel === 'function' && handel() === false) {
          e.returnValue = true;
          e.cancelBubble = true
        }
      })
    }
  }
  function _getCss(obj, style) {
    if (w.getComputedStyle) {
      return getComputedStyle(obj)[style]
    } else {
      return obj.currentStyle[style];
    }
  }
  class Jquery {
    constructor(selector) {
      this.elements = [];
      switch (typeof selector) {
        case 'string':
          this.elements = doc.querySelectorAll(selector);
          break;
        case 'function':
          _addEvent(w, 'load', selector);
          break;
        case 'object':
          if (selector instanceof Array) {
            this.elements = selector;
          } else {
            this.elements.push(selector)
          }
          break;
      }
    }
    each(fn) {
      const elements = this.elements;
      let ele = null;
      for (let i = 0; i < elements.length; i++) {
        ele = elements[i];
        fn(ele);
      }
    }
    html(str) {
      if (str) {
        this.elements[0].innerHTML = str;
      } else {
        return this.elements[0].innerHTML
      }
      return this;
    }
    attr(attr, value) {
      if (typeof attr === 'object') {
        for (let k in attr) {
          this.each(ele => ele.setAttribute(k, attr[k]))
        }
      } else {
        if (value) {
          this.each(ele => ele.setAttribute(attr, value))
        } else {
          return this.elements[0].getAttribute(attr);
        }
      }
      return this;
    }
    css(attr, value) {
      if (typeof attr === 'object') {
        for (let k in attr) {
          this.each(ele => ele.style[k] = attr[k])
        }
      } else {
        if (value) {
          this.each(ele => ele.style[attr] = value)
        } else {
          return _getCss(this.elements[0], attr);
        }
      }
      return this;
    }
    find(selector) {
      let arr = [];
      [].push.apply(arr, this.elements[0].querySelectorAll(selector));
      return $(arr);
    }
    eq(num) {
      return $(this.elements[num])
    }
    index() {
      const elements = this.elements[0].parentNode.children;

      let arr = [];
      [].push.apply(arr, elements);

      return [].indexOf.call(arr, this.elements[0]);
    }
    click(fn) {
      this.on('click', fn);
      return this;
    }
    on(type, fn) {
      this.each(ele => _addEvent(ele, type, fn.bind(ele)));
      return this;
    }
    hover(a, b) {
      this.on('mouseenter', a);
      this.on('mouseleave', b);
      return this;
    }
    show() {
      this.elements[0].style.display = ''
    }
    hide() {
      this.elements[0].style.display = 'none';
    }
    add() {
      console.log('哈哈');
      return this;
    }
    goo() {
      console.log('你怎么');
      return this;
    }
    removeAll(cname) {
      const elements = this.elements[0].parentNode.children;
      let ele = null;
      for (let i = 0; i < elements.length; i++) {
        ele = elements[i];
        if (ele.classList.contains(cname)) {
          ele.classList.remove(cname);
        }
      }
      return this;
    }
    addClass(cname) {
      this.elements[0].classList.add(cname);
      return this;
    }
    toggle(cname) {
      this.elements[0].classList.toggle(cname);
      return this;
    }
  }
  function $(selector) {
    return new Jquery(selector);
  }
  w.$ = $;
})(window, document)